<?php
	class MY_Controller extends CI_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->checkLogin();
			$this->checkPermission();
		}
		
		public function checkLogin(){
			$this->load->library('session');
			$logged = $this->session->userdata('logged_in');
			if(!isset($logged['username'])) redirect('', 'refresh');
		}
		
		public function checkPermission(){
			$this->load->library('session');
			$logged = $this->session->userdata('logged_in');
			if(count($logged)){
				$gid = $logged['gid'];
				$role = $logged['role'];
				
				if(!empty($gid) && $gid!=1){
					$current_route = $this->uri->segment(1);
					$current_method = $this->uri->segment(2);
					$flag_login = false;
					
					if($current_method!="logout" && count($role)>0){
						
						foreach($role as $v){
							
							if($current_route=="home"){
								$flag_login = true;
								break;
							}
							
							if(preg_match('/'.$current_route.'/', $v['route'])){
								$flag_login = true;
								break;
							}
							else continue;					
						}
						if(!$flag_login){ redirect('home', 'refresh'); }
					}
				}	
			}
			
		}
		
		public function pager($link, $total_row, $per_page, $segment=2){
			/*
				$segment = 2 => http://urlindex.php/controller/$page
				$segment = 3 => http://urlindex.php/controller/method/$page
				$segment = 4 => http://urlindex.php/controller/method/$param/$page
			*/
			$this->load->library('pagination');
			$config['base_url'] = $link;
			$config['total_rows'] = $total_row;
			$config['per_page'] = $per_page; 
			$config['cur_tag_open'] = '<b>';
			$config['cur_tag_close'] = '</b>';
			$config['full_tag_open'] = '<p>';
			$config['full_tag_close'] = '</p>';
			//$config['use_page_numbers'] = TRUE;
			$config['uri_segment'] =  $segment; 
			$this->pagination->initialize($config); 		
		}	 
		
	}	
	/**
	* MY_Eko 
	* 
	* MY_Eko is the controller, it check the user is logged in. 
	*  
	* @author Thang
	* @version $Id:  ads.php 2012-04-18
	* @access public 
	*
	*/
	class MY_Eko extends CI_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->checkLogin();			
		}
		
		/**
		* checkLogin(): 	check the user is logged in			
		* 
		* @param 
		* @return    
		* @access public
		*/
		public function checkLogin(){			
			$myusername = $this->session->userdata('myusername');
			if(!isset($myusername) || empty($myusername))
				redirect('eko', 'refresh');
		}		
	}
?>